//
//  TSCommonTool.swift
//  TSLiveWallpaper
//
//  Created by 100Years on 2024/12/20.
//

import Kingfisher
import Foundation
import AVFoundation

public let kMainQueue = DispatchQueue.main
/// 主线程延迟执行回调
/// - Parameters:
///   - delay: 延迟时间（秒）
///   - completion: 延迟后的回调
public func kDelayOnMainThread(_ delay: TimeInterval, completion: @escaping () -> Void) {
    DispatchQueue.main.asyncAfter(deadline: .now() + delay) {
        completion()
    }
}

public func kDelayMainShort(completion: @escaping () -> Void) {
    DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
        completion()
    }
}

/// 在主线程上执行代码
/// - Parameter block: 要执行的代码块
public func kExecuteOnMainThread(_ block: @escaping () -> Void) {
    DispatchQueue.main.async {
        block()
    }
}

/// 主线程延迟执行回调
/// - Parameters:
///   - delay: 延迟时间（秒）
///   - completion: 延迟后的回调
public func kMainAfter(_ delay: TimeInterval, completion: @escaping () -> Void) {
    DispatchQueue.main.asyncAfter(deadline: .now() + delay) {
        completion()
    }
}

public func kMainShort(completion: @escaping () -> Void) {
    DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
        completion()
    }
}

/// 在主线程上执行代码
/// - Parameter block: 要执行的代码块
public func kMainAsync(_ block: @escaping () -> Void) {
    DispatchQueue.main.async {
        block()
    }
}
/// 在主线程上执行代码
/// - Parameter block: 要执行的代码块
public func kGlobalAsync(_ block: @escaping () -> Void) {
    DispatchQueue.global(qos: .userInitiated).async {
        block()
    }
}

/// 子 size 根据容器 size,返回自己的最大 size
/// - Parameters:
///   - containerSize: 容器 Size
///   - subviewSize: 子 Size
///   - contentInset: contentInset
///   - contentMode: contentMode
/// - Returns: 最大 size
public func kCalculateScaledSize(containerSize: CGSize, subviewSize: CGSize, contentInset: UIEdgeInsets = .zero ,contentMode:UIView.ContentMode) -> CGSize {
    // 计算去除边距后的容器可用大小
    let availableWidth = containerSize.width - contentInset.left - contentInset.right
    let availableHeight = containerSize.height - contentInset.top - contentInset.bottom
    
    switch contentMode {
    case .scaleToFill:
        // 直接填充容器，忽略子视图比例
        return CGSize(width: availableWidth, height: availableHeight)
    case .scaleAspectFit:
        // 按比例适配容器，确保子视图完全显示在容器内
        let widthRatio = availableWidth / subviewSize.width
        let heightRatio = availableHeight / subviewSize.height
        let scale = min(widthRatio, heightRatio)
        return CGSize(width: subviewSize.width * scale, height: subviewSize.height * scale)
    case .scaleAspectFill:
        // 按比例填充容器，可能会裁剪子视图
        let widthRatio = availableWidth / subviewSize.width
        let heightRatio = availableHeight / subviewSize.height
        let scale = max(widthRatio, heightRatio)
        return CGSize(width: subviewSize.width * scale, height: subviewSize.height * scale)
    default:
        // 默认返回子视图原始大小
        return subviewSize
    }
}


public func kGetCollectionViewCellSize(sectionInset:UIEdgeInsets,
               itemSpacing:CGFloat,
               originalSize:CGSize,
               cellRowNum:Int)->CGSize{
    let widthReduction:CGFloat = 1.0
    let originalScale = originalSize.width/originalSize.height
    var w = k_ScreenWidth-sectionInset.left-sectionInset.right-widthReduction
    w = w - itemSpacing * CGFloat((cellRowNum-1))
    w = w/CGFloat(cellRowNum)
    let h = w/originalScale
    let cellSize = CGSizeMake(w, h)
    return cellSize
}
